﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace University.Workshop.Business
{
     public static  class RegistrationProduct
    {
        public static bool IsValidIdProduct(string idProduct)
        {
            if (string.IsNullOrEmpty(idProduct) || idProduct.Length < 9)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static bool IsValidNameProduct(string nameProduct)
        {
            if (string.IsNullOrEmpty(nameProduct))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool IsValidCost(string cost)
        {
            if (!string.IsNullOrEmpty(cost))
            {
               if (Regex.IsMatch(cost, @"[0-9]"))
                    return true;
                else
                    return false;
            }
            else 
            {
                return false;
            }
        }
        public static bool IsValidCount(string count)
        {
            if (!string.IsNullOrEmpty(count))
            {
                if (Regex.IsMatch(count, @"[0-9]"))
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }
        }
        public static string IsValidRoom(string room)
        {
            if (string.IsNullOrEmpty(room))
            {
                return null;
            }
            else
            {
                CheckInDataAccess  CheckRoom = new CheckInDataAccess();
                CheckRoom.Dispose();
                string exitRoom = CheckRoom.IsValidRoomCheckIn(room);
                if (exitRoom == null)
                {
                    return null;
                }
                else
                {
                    return exitRoom;
                }
            }
        }
        public static int RegisterProduct(string idProduct, string group, string nameProduct, string cost, string costSale, string amount, string dateSale)
        {
            ProductRegister RegisterNewProduct = new ProductRegister();
            RegisterNewProduct.Dispose();
            if (RegisterNewProduct.ExistProduct(idProduct))
            {
                return 1;
            }
            else
            {
                Product product = new Product { IdProduct = idProduct, Group = group, NameProduct = nameProduct, CostBuy = cost, CostSale=costSale , Amount=amount,DateSale=dateSale };
                RegisterNewProduct.RegisterProduct(product);
                return 0;
            }
        }

        public static int ValidateUpdateProducts(string idProductUpdate, string idProduct, string group, string name, string cost, string costSale, string amount, string dateSale)
        {
            ProductRegister registerProduct = new ProductRegister();
            registerProduct.Dispose();
            if(idProductUpdate != idProduct)
            {
                if (registerProduct.ExistProduct(idProduct))
                {
                    return 1;
                }
                else 
                {
                    Product product = new Product { IdProduct = idProduct, Group = group, NameProduct = name, CostBuy = cost, CostSale = costSale, Amount = amount, DateSale = dateSale };
                    registerProduct.UpdateProductsDataAccess(idProductUpdate, product);
                    return 0;
                }                
            }
            else
            {
                Product product = new Product { IdProduct = idProduct, Group = group, NameProduct = name, CostBuy = cost, CostSale = costSale, Amount = amount, DateSale = dateSale };
                registerProduct.UpdateProductsDataAccess(idProductUpdate, product);
                return 0;
            }
        }
        
        public static void GetAllProduct(DataGridView products)
        {
            ProductRegister productAccess = new ProductRegister();
            productAccess.Dispose();
            productAccess.GetAllProducts(products);
        }
        public static Product UpdateByproduct(string productUpdate)
        {
            try
            {
                ProductRegister upDateProduct = new ProductRegister();
                upDateProduct.Dispose();
                return upDateProduct.ViewUpdateProduct(productUpdate);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }
        public static void RegisterSale(string id, string idRoom, string costSaleProducts, string description)
        {
            ProductRegister RegisterNewSale = new ProductRegister();
            RegisterNewSale.Dispose();
            Sale sale = new Sale { Id = id, IdRoom = idRoom, CostSaleProducts = costSaleProducts, Description = description, DateOfSale = DateTime.Today };
            //Product product = new Product { IdProduct = idProduct, Group = group, NameProduct = nameProduct, CostBuy = cost, CostSale = costSale, Amount = amount, DateSale = dateSale };
            //RegisterNewProduct.RegisterProduct(product);
            RegisterNewSale.RegisterSale(sale);
        }

        public static float GetCost(String roomName, DateTime checkIn, DateTime checkout)
        {
            ProductRegister productDal = new ProductRegister();
            productDal.Dispose();
            float parcialCost = productDal.GetCost(roomName, checkIn, checkout);
            return parcialCost;
        }
    }
}
